<?php
class WdkPlugin {
	
	protected $config;
	protected $plg_name;
	protected $tpl;
	protected $skin;
	protected $param;
	protected $domain;
	protected $loaded_skins = array();

	function __construct($name)
	{
		$this->plg_name = $name;
		$this->config = Wdk::config();
		
		$this->tpl = new Template_();
		$this->tpl->compile_dir = CACHE_PATH.'tpl_/_compile';
		$this->tpl->template_dir = PLGPATH."{$this->plg_name}/";
		$this->tpl->cache_dir = CACHE_PATH.'tpl_/_cache';
	}

	public function __init($param)
	{
		$this->param = $param;
		if(isset($this->param['skin']))
		{
			$this->skin = $this->param['skin'];
		}
		else
		{
			$this->param['skin'] = $this->skin = 'default';
		}

		$this->tpl->define($this->plg_name, "{$this->skin}/index.html");
		######################################################
		$this->tpl->assign('config', $this->config);
		//$this->tpl->assign('session', $ci->session->userdata);
		######################################################

		$this->checkeLoaded();
		foreach($param as $k=>$v)
		{
			if(isset($this->{$k}))	$this->{$k} = $v;
		}
	}

	private function checkeLoaded()
	{
		$is_first_loading = 0;
		if(in_array($this->skin, $this->loaded_skins) == false)
		{
			$is_first_loading = 1;
			array_push($this->loaded_skins, $this->skin);
		}
		$this->tpl->assign('is_first_loading', $is_first_loading);
	}

	public function exec()
	{
		$this->param['SKINDIR'] = str_replace(BASE_PATH, '', PLGPATH)."{$this->plg_name}/{$this->skin}/";
		$this->tpl->assign($this->param);
		$content = $this->tpl->fetch($this->plg_name);
		return $content;
	}
}